package com.ist.word.main;

import com.ist.word.audio.AudioPlayer;
import com.ist.word.logic.GameConfigurations;
import com.ist.word.ui.GameMainUI;
import com.ist.word.ui.GameMenuList;
import com.ist.word.ui.GameSplashScreen;

public class GameMainApplication implements Runnable {
	GameSplashScreen gameSplashScreen = new GameSplashScreen();
	GameMenuList gameMenuList=new GameMenuList();;
	GameMainUI gameMainUI=new GameMainUI();
	AudioPlayer GameMainAudioPlayer=new AudioPlayer(GameConfigurations.GAMEMAINMUSIC_BG);
	AudioPlayer GameMenuAudioPlayer=new AudioPlayer(GameConfigurations.GAMEMENUMUSIC_BG);

	GameMainApplication() {
	}

	public static void main(String[] args) {
		
		Thread mainThread = new Thread(new GameMainApplication());

		mainThread.start();
	}

	@Override
	public void run() {
		
		
		while (true) {

			int gameSplashScreenStatus = gameSplashScreen.getGameSplashScreenStatus();
			int newGameFlag = gameMenuList.getNewGameFlag();
			int returnToMenuListFlag=gameMainUI.getReturnToMenuListFlag();
			int continueGameFlag=gameMenuList.getContinueGameFlag();
			
			try {
				Thread.sleep(100);
				if (gameSplashScreenStatus == 1) {
					gameSplashScreen.setGameSplashScreenStatus(0);
					gameSplashScreen.dispose();
					gameMenuList.init();
					gameMenuList.doLay();
					GameMenuAudioPlayer.loop();
					gameMainUI.initUI();
					gameMainUI.doLay();
					gameMainUI.dispose();
				}
				if(newGameFlag==1){
					GameMenuAudioPlayer.reset();
					GameMainAudioPlayer.reset();
					GameMainAudioPlayer.loop();
					gameMenuList.setNewGameFlag(0);
					gameMainUI.setVisible(true);
					gameMenuList.setVisible(false);
					gameMainUI.intialVariables();
				}
				if(continueGameFlag==1){
					GameMenuAudioPlayer.reset();
					GameMainAudioPlayer.loop();
					gameMenuList.setContinueGameFlag(0);
					gameMainUI.setVisible(true);
					gameMainUI.resumeGameTimer();
					gameMenuList.setVisible(false);
				}
				
				if(returnToMenuListFlag==1){
					GameMenuAudioPlayer.reset();
					GameMenuAudioPlayer.play();
					GameMainAudioPlayer.pause();
					gameMainUI.setReturnToMenuListFlag(0);
					gameMainUI.setVisible(false);
					gameMenuList.setVisible(true);
				}
				//System.out.println("returnToMenuListFlag"+returnToMenuListFlag);
				//System.out.println("newGameFlag"+newGameFlag);
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
